# -*- coding: utf-8 -*-

import os
os.chdir("..")

from preprocess import my_word2vec

f = open('../Data/interest.csv')
lines = f.readlines()

#map{key,string}
catagroies = {}
for line in lines:
    s = line.split(',')
    if len(s) == 2:
        catagroies[s[0].strip()] = s[1].strip()
    else:
        catagroies[s[0].strip()] = ''

#id=interest.sports.baseball
#find all parents_id= interest.sports + interest
def get_all_parents_id(id):
    #global catagroies
    sub_k_in_list = id.split('.')

    all_string = []
    for i in range(2, len(sub_k_in_list) + 1):
        substring = '.'.join(sub_k_in_list[:i])
        all_string.append(substring)
    return all_string

"""

#id=interest.sports
#find _id= interest.sports.*
def get_all_subs_id(id):
    global catagroies
    substring = []
    for k in catagroies.keys():
        if k != id and k.startswith(id):
            substring.append(substring)
    return substring

"""
#id=interest.sports
#find _id= interest.* -interest.sports - interest.sports.*

def get_all_same_level_ids(id):
    global catagroies
    sub_k_in_list = id.split('.')
    sub_id = '.'.join(sub_k_in_list[:-1])
    substring = []
    for k in catagroies.keys():
        if k.startswith(id) == False and k.startswith(sub_id) and id.startswith(k) == False:
            substring.append(k)
    return substring

def ids_2_words(ids):
    global catagroies
    words = []
    for id in ids:
        w = catagroies[id].strip()
        if len(w) == 0:
            continue
        words.extend(w.split(' '))

    words = list(set(words))
    return words

def get_pos_and_neg_words(id):
    global catagroies
    pos_ids = get_all_parents_id(id)
    pos_ids.append(id)

    pos_words = ids_2_words(pos_ids)

    neg_ids = get_all_same_level_ids(id)

    neg_words = ids_2_words(neg_ids)
    return pos_words,neg_words

def word_list2unicode(words):
    unicode_list = []
    for w in words:
        unicode_list.append(w.decode('utf-8'))
    return  unicode_list
"""
#merge string by catagroies(interest.sports.baseball =  interest.sports.baseball + interest.sports + interest)
new_catagroies={}
for k in catagroies.keys():
    sub_k_in_list = k.split('.')
    substring = ''
    all_string = ''
    for i in range(2,len(sub_k_in_list)+1):
        substring = '.'.join(sub_k_in_list[:i])
        all_string = all_string + ' ' + catagroies[substring]
    new_catagroies[k] = all_string

#add all subs (interest.sports = interest.sports +  interest.sports.*)
#key:(catagroies,others)
new1_catagroies={}
for k in catagroies.keys():
    all_words = []
    for k1 in catagroies.keys():
        if k1.startswith(k):
            all_words.append(k1)

    new1_catagroies[k] = all_words


#add others to catagroies(others =  interest.sports.others - interest.sports.baseball)
#key:(catagroies,others)
"""


word_vector_model = '../model/news-163_word_vector.bin'

wv = my_word2vec.Convert2Vector(word_vector_model)




csv_dict =[]
for key in catagroies.keys():
    if key == '':
        continue
    pos_words, neg_words = get_pos_and_neg_words(key)
    pos_words = word_list2unicode(pos_words)
    neg_words = word_list2unicode(neg_words)

    if key == 'interest.sports.billiards':
        pass
    close_words = wv.get_close_words(pos=pos_words,num=1000)

    neg_close_words = wv.get_close_words(pos=neg_words, num=1000)
    neg_close_words.extend(neg_words)
    tmp_close_words = []

    for w in close_words:
        if w not in neg_close_words:
            tmp_close_words.append(w)

    close_words = tmp_close_words

    close_words = ' '.join(close_words).encode('utf-8')
    d = {'id':key,'features':catagroies[key],'close_words':close_words}
    csv_dict.append(d)

import csv

f=open('../Data/interest_features.csv','w')
csv_f = csv.DictWriter(f,fieldnames=['id','features','close_words'])
csv_f.writerows(csv_dict)
f.close()
pass

